home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Utilities / Winter Shell 1.0d2 / Source / Libraries / FileLib / FilePathLib.c < prev   
Encoding:
C/C++ Source or Header  |  1993-03-22  |  2.2 KB  |  86 lines  |  [TEXT/KAHL]

  1. /* These functions for getting the "Poor Man's Search Path" were
  2.     adapted from TechNote #238. These functions won't (or
  3.     aren't supposed to) work under AUX 1.1, but I don't wan't
  4.     to worry about buggy Apple software which has probably
  5.     been fixed for AUX 2.0 (though the delimiter is always
  6.     ':' while in AUX it's a '/').
  7.     
  8.     I have not tested these functions since I don't need them. */
  9.  
  10. #include <limits.h>
  11. #include <string.h>
  12. #include "MemoryLib.h"
  13. #include "StringLib.h"
  14. #include "FileLib.h"
  15.  
  16. /* Given a directory ID and volume reference number this constructs
  17.     a pathname specifying the directory. */
  18. OSErr FilePathFromDir(FileDirType dir, FileVolType vol, FilePathType path)
  19. {
  20. BEGIN
  21.     CInfoPBRec        pb;
  22.     FilePathType    dirname;
  23.     FilePathType    fname;
  24.     short                length;
  25.     OSErr                err = noErr;
  26.     
  27.     /* save file name and its length */
  28.     strcpy(path, fname);
  29.     if (*fname == ':')
  30.         memmove(fname, fname+1, strlen(fname));
  31.     length = strlen(fname);
  32.     *path = 0;
  33.     
  34.     /* initialize parameter block */
  35.     *dirname = 0;
  36.     memset(&pb, 0, sizeof(pb));
  37.     pb.dirInfo.ioNamePtr = (StringPtr) dirname;
  38.     pb.dirInfo.ioDrParID = dir;
  39.     
  40.     /* append the names of each parent directory until we reach the root */
  41.     do {
  42.         pb.dirInfo.ioVRefNum = vol;
  43.         pb.dirInfo.ioFDirIndex = -1;
  44.         pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  45.         err = PBGetCatInfo(&pb, false);
  46.         
  47.         /* append name of directory after making sure there's room in string */
  48.         if (! err) {
  49.             PtoCstr(dirname);
  50.             if (length + strlen(dirname) + 1 < sizeof(FilePathType)-1) {
  51.                 strcat(dirname, ":");
  52.                 strcpy(path, dirname);
  53.                 CtoPstr(dirname);
  54.             }
  55.             else
  56.                 err = bdNamErr;
  57.         }
  58.         
  59.     } while (! err && pb.dirInfo.ioDrDirID != fsRtDirID);
  60.     
  61.     /* append original file name */
  62.     if (! err)
  63.         strcat(path, fname);
  64.     
  65.     return(err);
  66. END
  67. }
  68.  
  69. /* Given a working directory and a partial path name this builds the full
  70.     path name. The working directory could be a volume reference number
  71.     such as that returned by SFGetFile. */
  72. OSErr FilePathFromWD(FileWDirType wd, FilePathType path)
  73. {
  74. BEGIN
  75.     FileDirType    dir;
  76.     FileVolType    vol;
  77.     long            procid;
  78.     OSErr            err = noErr;
  79.     
  80.     err = GetWDInfo(wd, &vol, &dir, &procid);
  81.     if (! err)
  82.         err = FilePathFromDir(dir, vol, path);
  83.     return(err);
  84. END
  85. }
  86.